*cd /data2/cedsei/data2/

global input_dir  "/data/rawdata/cleaned"
global output_dir "/data2/cedsei/data2/"


foreach num of numlist 2005(1)2018 {
	clear
	use /data/rawdata/cleaned/aku_`num'	
	
	gen search = cond(!mi(uarbsok),cond(uarbsok==1,1,0), ///
	cond(!mi(syssokt3),cond(syssokt3==0,1,0),.))
	
	gen search_w = cond(!mi(uarbsokveckor),uarbsokveckor, ///
	cond(!mi(syssokv), syssokv, ///
	cond(!mi(uarbsok) & uarbsok==2,0, ///
	cond(!mi(syssokt3) & syssokt3==3,0,.))))
	replace search_w=. if search_w>=800 
	// 0 means that they did not search for the past 4 weeks.
	// Maybe censor these at 52 weeks? 
	
	gen search_afcoach   = cond(!mi(syssoksatt1), cond(syssoksatt1>0,1,0), cond(!mi(uarbsoksatt1),cond(uarbsoksatt1>0,1,0),.)) 
	// searching through AF with a coach.
	// not defined for those who do not search
	
	gen search_afself    = cond(!mi(syssoksatt2), cond(syssoksatt2>0,1,0), cond(!mi(uarbsoksatt2),cond(uarbsoksatt2>0,1,0),.)) 
	// searching through AF without a coach.
	
	gen search_databa    = cond(!mi(syssoksatt3), cond(syssoksatt3>0,1,0), cond(!mi(uarbsoksatt3),cond(uarbsoksatt3>0,1,0),.)) 
	// searching through other job-databases.
	
	gen search_recruit   = cond(!mi(syssoksatt4), cond(syssoksatt4>0,1,0), cond(!mi(uarbsoksatt4),cond(uarbsoksatt4>0,1,0),.)) 
	// searching by contacting recruiters.
	
	gen search_direct   = cond(!mi(syssoksatt5), cond(syssoksatt5>0,1,0), cond(!mi(uarbsoksatt5),cond(uarbsoksatt5>0,1,0),.)) 
	// searching by directly contacting employers.
	
	gen search_postads  = cond(!mi(syssoksatt6), cond(syssoksatt6>0,1,0), cond(!mi(uarbsoksatt6),cond(uarbsoksatt6>0,1,0),.)) 
	// searching by posting / replying to ads.
	
	gen search_readads  = cond(!mi(syssoksatt7), cond(syssoksatt7>0,1,0), cond(!mi(uarbsoksatt7),cond(uarbsoksatt7>0,1,0),.)) 
	// searching by reading ads.
	
	gen search_friends  = cond(!mi(syssoksatt8), cond(syssoksatt8>0,1,0), cond(!mi(uarbsoksatt8),cond(uarbsoksatt8>0,1,0),.)) 
	// searching by contacting friends.
	
	// gen search_commun   = cond(!mi(syssoksatt9), syssoksatt9, cond(!mi(uarbsoksatt9),uarbsoksatt9,.)) 
	// searching through school and municipality.

	gen search_interview = cond(!mi(syssoksatt10), cond(syssoksatt10>0,1,0), cond(!mi(uarbsoksatt10),cond(uarbsoksatt10>0,1,0),.)) 
	// Went to job interview
	
	gen search_int = search_afcoach + search_afself + search_databa + search_recruit + search_direct + search_postads + search_readads + search_friends + search_interview
	
	save /home/projects/cedsei/data/rawdata/searchaku_v2_`num', replace
	
	if `num'>2005 {
		append using /home/projects/cedsei/data/rawdata/searchaku
	}
	save /home/projects/cedsei/data/rawdata/searchaku, replace 
}


use lopnr date year month  year search* refveckar using "$rawdatapath/searchaku_v2_2005.dta",clear
foreach num of numlist 2006(1)2018 {
	append using "$rawdatapath/searchaku_v2_`num'.dta", keep(lopnr date year month  year search* refveckar)
	
	
}

	
replace date = ym(year(date),month(date))
rename refveckar ref_week
destring ref_week,replace
rename lopnr persid
save	"$datapath/AKUsearch.dta",replace	
